/*
 * Copyright 2010 Alessandro Bellucci
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.soupframework.gwt.designerpanel.interaction.mouse.client;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.soupframework.gwt.designerpanel.client.designer.impl.VerticalPanelViewDesigner;
import org.soupframework.gwt.designerpanel.client.interaction.InteractionHandlerManager;
import org.soupframework.gwt.designerpanel.client.util.Util;

import com.google.gwt.event.dom.client.MouseEvent;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.user.client.ui.Widget;

public class VerticalPanelMouseInteractionHandler<T, V extends VerticalPanelViewDesigner<T>> extends ContainerMouseDnDInteractionHandler<T, V> {

	public VerticalPanelMouseInteractionHandler(InteractionHandlerManager<T> manager, V designer) {
		super(manager, designer);
	}

	@Override
	protected boolean drag(MouseEvent<? extends EventHandler> event, boolean forceMove, boolean blockGridAlignment){
		boolean ret = super.drag(event, forceMove, blockGridAlignment);

		int deltaX = mouseX - lastHandledMouseX;
		int deltaY = mouseY - lastHandledMouseY;
		if(forceMove || deltaX != 0 || deltaY != 0){
			List<T> selectedObjects = this.getDesigner().getSelectedObjects(false);
			if(selectedObjects != null && selectedObjects.size() == 1){
				T selectedObject = selectedObjects.get(0);
				Widget sw = getDesigner().getChildObjectView(selectedObject);
				T targetObject = null;
				int y = mouseY;

				if(y < sw.getAbsoluteTop()){
					// moving up
					for(T child : getDesigner().getChildrenObjects()){
						Widget w = getDesigner().getChildObjectView(child);
						int oy = w.getAbsoluteTop() + w.getOffsetHeight() - Util.getTotalWidgetScrollTop(w);
						if(oy >= y){
							targetObject = child;
							break;
						}
					}
				}
				else if(y > sw.getAbsoluteTop() + sw.getOffsetHeight()){
					// moving down
					List<T> childrenObjects = new ArrayList<T>(getDesigner().getChildrenObjects());
					Collections.reverse(childrenObjects);
					for(T child : childrenObjects){
						Widget w = getDesigner().getChildObjectView(child);
						int oy = w.getAbsoluteTop() - Util.getTotalWidgetScrollTop(w);
						if(oy <= y){
							targetObject = child;
							break;
						}
					}
				}

				if(targetObject != null && selectedObject != targetObject){
					this.getDesigner().exchangeObjectsPartial("" + this.mouseDownEventCount, selectedObject, targetObject);
				}

				return true;
			}
		}
		return ret;
	}

	@Override
	protected void addObjectInt(T newObject, int relativeMouseX, int relativeMouseY) {
		this.getDesigner().addObject(newObject, relativeMouseX, relativeMouseY);
	}
}
